var index = 0;
var refund = false;
var finalStep = false;

function refundOrder() {
    let table = document.querySelector('tbody.xianshishujudate');
    if (table) {
        let orders = table.querySelectorAll('td.tableOrderId');
        if (orders) {
            let order = orders[index];
            index++;
            $("#progress")[0].textContent = index + "/" + orders.length;
            if (order) {
                let span = order.querySelector('span.refund');
                if (span) {
                    span.click();
                } else {
                    refundOrder();
                }
            } else {
                complete();
            }
        } else {
            complete();
        }
    } else {
        complete();
    }
}

function complete() {
    refund = false;
    index = 0;
    $("#progress")[0].textContent = "完成";
}

function check() {
    let input = document.querySelector('input#searchContent')
    if (input) {
        let searchOrderNumber = input.value
        if (searchOrderNumber) {
            let table = document.querySelector('tbody.xianshishujudate')
            if (table) {
                let orders = table.querySelectorAll('td.tableOrderId')
                if (orders) {
                    orders.forEach(
                        function (order) {
                            let a = order.querySelector('a.limingcentUrlpic')
                            if (a) {
                                let orderNumber = a.text
                                if (orderNumber.match(searchOrderNumber) || searchOrderNumber.match(orderNumber)) {
                                    let span = order.querySelector('span.refund')
                                    if (!span) {
                                        let action = a.getAttribute('onclick')
                                        order.innerHTML = order.innerHTML + '<br><span style="font-size: 12px;color: red;" class="refund" onclick="document.querySelector(\'step1\').textContent=\'' + orderNumber + '\';' + action + '">退款</span>'
                                    }
                                }
                            }
                        }
                    )
                }
            }

            let showButton = $("span.refund").length > 0
            let oneKeyReturn = $("span#oneKeyReturn")[0]
            if (showButton) {
                if (!oneKeyReturn) {
                    let location = $("ul#upPage")[0];
                    if (location) {
                        $(location).before("<span id='oneKeyReturn' style='font-size: 16px;color: red;' onclick='$(\"temp\").append(\"<onekeyreturn></onekeyreturn>\");'>一键退款</span><span id='progress'></span>");
                    }
                }
            } else {
                complete();
                if (oneKeyReturn) {
                    $(oneKeyReturn).remove();
                }
            }

            let onekeyreturn = $('onekeyreturn')[0];
            if (onekeyreturn) {
                $(onekeyreturn).remove();
                if (!refund) {
                    refund = true;
                    index = 0;
                    refundOrder();
                }
            }

            let step1 = document.querySelector('step1')
            let stepOrderNumber1 = step1.textContent
            if (stepOrderNumber1) {
                let dialog1 = document.querySelector("div#orderDetails.modal.in")
                if (dialog1) {
                    dialog1.querySelectorAll('td strong').forEach(
                        function (text) {
                            if (text.textContent.match(stepOrderNumber1)) {
                                step1.textContent = ""
                                var button;
                                dialog1.querySelectorAll("button.btn.btn-primary").forEach(
                                    function (b) {
                                        if (b.getAttribute('onclick') === "showRefund();") {
                                            button = b;
                                        }
                                    }
                                )
                                if (button) {
                                    document.querySelector('step2').textContent = stepOrderNumber1
                                    button.click()
                                } else {
                                    if (refund) {
                                        refundOrder();
                                    }
                                }
                            }
                        }
                    )
                }
            }
            let step2 = document.querySelector('step2')
            let stepOrderNumber2 = step2.textContent
            if (stepOrderNumber2) {
                let dialog1 = document.querySelector("div#orderDetails.modal.in")
                if (dialog1) {
                    dialog1.querySelectorAll('td strong').forEach(
                        function (text) {
                            if (text.textContent.match(stepOrderNumber2)) {
                                step2.textContent = ""
                                let dialog2 = document.querySelector("div#tuikuan.modal.in")
                                if (dialog2) {
                                    var button;
                                    dialog2.querySelectorAll("button.btn.btn-primary").forEach(
                                        function (b) {
                                            if (b.getAttribute('onclick') === "refundOrder();") {
                                                button = b;
                                            }
                                        }
                                    )
                                    if (button) {
                                        finalStep = true;
                                        button.click();
                                    } else {
                                        if (refund) {
                                            refundOrder();
                                        }
                                    }
                                }
                            }
                        }
                    )
                }
            }

            if (refund) {
                if (finalStep) {
                    let dialog2 = document.querySelector("div#tuikuan.modal.in");
                    if (!dialog2) {
                        refundOrder();
                    }
                }
            }
        }
    }
}

$(document).ready(function () {
    $(document.body).append("<temp><step1></step1><step2></step2></temp>");
    setInterval(check, 1000)
})